<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Dynamic;
use App\Models\BranchCourt;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class DynamicController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Dynamic(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('dynamic_nick');
            $grid->column('dynamic_img')->image();
            $grid->column('dynamic_text');
            $grid->column('hospital_id','分院')->display(function ($query){
                return BranchCourt::where('id',$query)->value('hospital_nick');
            });
//            $grid->column('status')->radio([1=>'通过',2=>'驳回',0=>'待审核']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Dynamic(), function (Show $show) {
            $show->field('id');
            $show->field('dynamic_nick');
            $show->field('dynamic_img');
            $show->field('dynamic_text');
            $show->field('hospital_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Dynamic(), function (Form $form) {
            $form->display('id');
            $form->text('dynamic_nick');
            $form->image('dynamic_img')->disk('qiniu')->saveFullUrl()->uniqueName()->required();
            $form->textarea('dynamic_text');
            $form->select('hospital_id','分院')->options('hospital');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    /**
     * @return mixed
     * 查询分院信息
     */
    function hospital(){
        $data=BranchCourt::get(['id','hospital_nick']);
        foreach ($data as $k=>$v){
            $data[$k]['text']=$v['hospital_nick'];
        }
        return $data;
    }
}
